//
//  NewFeatureViewController.swift
//  weibo10
//
//  Created by 晁岳山 on 15/10/18.
//  Copyright © 2015年 晁岳山. All rights reserved.
//

import UIKit
/**
1.实现init方法  设置layout 设置layout属性， 设置collectionView属性的时候，要在super.init
之后
2.实现数据源方法
3.自定义cell
4.最后一张图片实现按钮的效果
注意：给按钮添加点击事件的时候，addtart的时候要 加上@objc  不然程序会崩溃  原因：事件点击不能加入运行循环
*/

private let newFeatureViewID = "NewFeatureViewID"
private let newFeatureImageCount = 4
class NewFeatureViewController: UICollectionViewController {
    //1.设置layout
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: newFeatureViewID)
    }
   
}

// MARK: - 数据源方法
extension NewFeatureViewController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return newFeatureImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(newFeatureViewID, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
    //scrollView停止滚动的时候调用这个方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //当时第四张图片的时候，显示btn 并且设置btn的动画
        let pageIndex = Int (scrollView.contentOffset.x / scrollView.bounds.width)
        if pageIndex  == (newFeatureImageCount - 1) {
            //开始动画  首先拿到cell, 调用动画方法
            let indexPath = NSIndexPath(forItem: pageIndex, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as? NewFeatureCell
            cell?.buttonStartAnimation()
        }
    }
    
}



//2.cell的样式
private class NewFeatureCell: UICollectionViewCell {
    //设置图片
    private lazy var iconView: UIImageView = UIImageView()
    //设置按钮
    private lazy var button: UIButton = UIButton(backImageName: "new_feature_finish_button", btnName: "立即体验", color: UIColor.whiteColor())
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named:"new_feature_\(imageIndex + 1)")
            button.hidden = true
        }
    }
    /**
    cell的重用问题  还有就是动画有点问题  开始是显示的
    */
    //button播放动画
    func buttonStartAnimation() {
        
        //缩放系数设置为0
        button.transform = CGAffineTransformMakeScale(0, 0)
        button.hidden = false
        button.userInteractionEnabled = false
        UIView.animateWithDuration(1,   //动画时长
            delay: 0,                   //延迟时间
            usingSpringWithDamping: 0.7, //弹力系数  0~1 越小越弹
            initialSpringVelocity: 10,  //初始速度
            options: [],  //动画选项
            animations: { () -> Void in
                
                self.button.transform = CGAffineTransformIdentity
                
            }) { (_) -> Void in
                self.button.userInteractionEnabled = true
        }
        
    }
    //设置界面布局
    func setUI() {
        addSubview(iconView)
        addSubview(button)
        iconView.frame = bounds
        button.sizeToFit()
        button.hidden = true
        //设置自动布局
        button.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        //添加按钮点击事件
        button.addTarget(self, action: "btnClick", forControlEvents: .TouchUpInside)
    }
    //按钮点击事件
   @objc private func btnClick() {
    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotifycation, object: "welcome")
    }
}



